package devmento.gae.dao;


import javax.jdo.PersistenceManager;

import org.springframework.stereotype.Component;

import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;

import devmento.gae.PMF;
import devmento.gae.model.Article;
import devmento.gae.model.User;


@Component
public class ArticleDaoJdo {
	
	public void create(Article article){
		PersistenceManager pm = PMF.get().getPersistenceManager();
		
		try{
			pm.makePersistent(article);
		}finally{
			pm.close();
		}
	}
	
	public Article find(long id){
		PersistenceManager pm = PMF.get().getPersistenceManager();
				
		Article detached = null;
		
		try{
			Article article = pm.getObjectById(Article.class, id);
			detached = pm.detachCopy(article);
		}finally{
			pm.close();
		}
		
		return detached;
	}
	
	public void delete(long id){
		PersistenceManager pm = PMF.get().getPersistenceManager();
						
		try{
			Article a = pm.getObjectById(Article.class, id);
			pm.deletePersistent(a);
		}finally{
			pm.close();
		}
	}
	
	public void update(String content, long id){
		PersistenceManager pm = PMF.get().getPersistenceManager();
		
		try{
			Article a = pm.getObjectById(Article.class, id);
			a.setContent(content);
			pm.makePersistent(a);
		}finally{
			pm.close();
		}
	}
	

}
